package ljt.music.utils;

import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.app.DownloadManager.Query;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.widget.Toast;

/**
 * Created by paigunan on 2018/1/6.
 */

public class DownloadUtils {
  //下载器
  private DownloadManager downloadManager;
  //上下文
  private Context mContext;
  //下载的ID
  private long downloadId;

  public DownloadUtils(Context context) {
    this.mContext = context;
  }

  //下载apk
  public void downloadAPK(String name, String title, String descroption, String path, String url) {

    //创建下载任务
    Request request = new Request(Uri.parse(url));
    //移动网络情况下是否允许漫游
    request.setAllowedOverRoaming(false);

    //在通知栏中显示，默认就是显示的
    request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
    request.setTitle(title);
    request.setDescription(descroption);
    request.setVisibleInDownloadsUi(true);

    //设置下载的路径
    request.setDestinationInExternalPublicDir(path, name);

    //获取DownloadManager
    downloadManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
    //将下载请求加入下载队列，加入下载队列后会给该任务返回一个long型的id，通过该id可以取消任务，重启任务、获取下载的文件等等
    downloadId = downloadManager.enqueue(request);

    //注册广播接收者，监听下载状态
    mContext.registerReceiver(receiver,
        new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
  }

  //广播监听下载的各个状态
  private BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
      checkStatus();
    }
  };


  //检查下载状态
  private void checkStatus() {
    DownloadManager.Query query = new Query();
    //通过下载的id查找
    query.setFilterById(downloadId);
    Cursor c = downloadManager.query(query);
    if (c.moveToFirst()) {
      int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
      switch (status) {
        //下载暂停
        case DownloadManager.STATUS_PAUSED:
          Toast.makeText(mContext, "下载暂停", Toast.LENGTH_SHORT).show();
          break;
        //下载延迟
        case DownloadManager.STATUS_PENDING:
          Toast.makeText(mContext, "下载延迟", Toast.LENGTH_SHORT).show();
          break;
        //正在下载
        case DownloadManager.STATUS_RUNNING:
          break;
        //下载完成
        case DownloadManager.STATUS_SUCCESSFUL:
          //下载完成安装APK
          Toast.makeText(mContext, "下载成功", Toast.LENGTH_SHORT).show();
          break;
        //下载失败
        case DownloadManager.STATUS_FAILED:
          Toast.makeText(mContext, "下载失败", Toast.LENGTH_SHORT).show();
          break;
      }
    }
    c.close();
  }

}
